package com.weasel.weixin.commons.session.dao;

import com.weasel.weixin.commons.session.SimpleWeixinSession;
import com.weasel.weixin.commons.session.WeixinSession;
import org.apache.commons.lang.StringUtils;

import java.util.UUID;

/**
 * Created by Dylan on 2015/12/8.
 */
public abstract class AbstractSessionDAO implements SessionDAO{

    @Override
    public void create(WeixinSession session) {
        if(StringUtils.isEmpty(session.getId())){
            assignSessionId(session,createSessionId());
        }
        doCreate(session);
    }

    protected String createSessionId(){
        return UUID.randomUUID().toString();
    }

    protected void assignSessionId(WeixinSession session,String sessionid){
        if(session instanceof SimpleWeixinSession){
            ((SimpleWeixinSession) session).setId(sessionid);
        }
    }

    protected abstract void doCreate(WeixinSession session);
}
